unit Dos.ORM.DataUtils;

interface

uses
  System.SysUtils, System.TypInfo, Dos.ORM.Field, Dos.ORM.Expression, rtti;

type
  TDataUtils = class
    class function FormatSQL(sql: string; leftToken, rightToken: string): string; static;
    class function FormatValue(value: TValue): string; overload;
    class function FormatValue(value: TField): string; overload;
    class function ToString(op: QueryOperator): string;
    class function MakeUniqueKey(field: TField): string;
    class function GetNewParamCount(): Integer;
  end;

implementation

var
  paramCount: Integer;




{ TDataUtils }

class function TDataUtils.FormatSQL(sql: string; leftToken, rightToken: string): string;
begin
  if sql = '' then
    Result := sql
  else
    sql := sql.Replace('{0}', leftToken).Replace('{1}', rightToken);

  result := sql;
end;

class function TDataUtils.FormatValue(value: TValue): string;
begin
  if (value.IsEmpty) then
    Result := 'null';
 // if value.TypeInfo.Name. then


end;

class function TDataUtils.FormatValue(value: TField): string;
begin
  Result := value.TableFieldName;
end;

class function TDataUtils.GetNewParamCount: Integer;
begin
  if paramCount >= 9999 then
    paramCount := 0;
  Inc(paramCount);
  Result := paramCount;

end;

class function TDataUtils.MakeUniqueKey(field: TField): string;
begin
  Result:=Concat('@', field.tableName, '_', field.Name, '_', GetNewParamCount().ToString);
end;

class function TDataUtils.ToString(op: QueryOperator): string;
begin
  case op of
    Equal:
      Result := ' = ';
    NotEqual:
      result := ' <> ';
    Greater:
      Result := ' > ';
    Less:
      Result := ' < ';
    GreaterOrEqual:
      Result := ' >= ';
    LessOrEqual:
      Result := ' <= ';
    Like:
      Result := ' like ';
    BitwiseAND:
      Result := ' & '; //???
    BitwiseOR:
      Result := ' | '; //??
    BitwiseXOR:
      Result := ' ^ '; //??
    BitwiseNOT:
      Result := ' ~  '; //???
    IsNULL:
      Result := ' is null ';
    IsNotNULL:
      Result := ' is not null ';
    Add:
      Result := ' + ';
    Subtract:
      Result := ' - ';
    Multiply:
      Result := ' * ';
    Divide:
      Result := ' / ';
    Modulo:
      Result := ' % ';
  end;

end;

end.

